#include "imageprovider.h"
#include "source/logger.h"
#include "ImageProcessor.h"
#include "tools.h"

ImageProvider &ImageProvider::getInstance()
{
    static ImageProvider _instance;
    return _instance;
}

ImageProvider::ImageProvider(QObject* parent) : QObject(parent),
    QQuickImageProvider(QQuickImageProvider::Image), imageCounter(0)
{
    m_imagemainInfo << 0 << 0 << 0;
}

QImage ImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
    Q_UNUSED(requestedSize);

    if (id.startsWith("Box1")) {
        if (size) *size = box1Image.size();
        return box1Image;
    } else if (id.startsWith("Box2")) {
        if (size) *size = box2Image.size();
        return box2Image;
    } else if (id.startsWith("Main")) {
        if (size) *size = mainImage.size();
        return mainImage;
    } else if (id.startsWith("pop1")) {
        if (size) *size = popImage.size();
        return popImage;
    } else if (id.startsWith("pop2")) {
        if (size) *size = pop2Image.size();
        return pop2Image;
    } else if (id.startsWith("popEye")) {
        if (size) *size = pop2EyeImage.size();
        return pop2EyeImage;
    }

    return QImage();
}


void ImageProvider::image2qml(const cv::Mat& image, const QString &target , const ImageDisplay step)
{
    if (!image.empty())
    {
        QImage qImage = convertToQImage(image);
        QUrl imageUrl;
        if (target == "Box1") {
            //n  LogDebug << "Box1";
            box1Image = qImage;
            imageUrl = QUrl(QString("image://imageProvider/Box1?%1").arg(imageCounter++));
            emit box1ImageReady(imageUrl);
        } else if (target == "pop") {
            popImage = qImage;
            imageUrl = QUrl(QString("image://imageProvider/pop1?%1").arg(imageCounter++));
            emit popImageReady(imageUrl);
        } else if (target == "pop2") {
            pop2Image = qImage;
            imageUrl = QUrl(QString("image://imageProvider/pop2?%1").arg(imageCounter++));
            emit pop2ImageReady(imageUrl);
        } else if (target == "popEye") {
            pop2EyeImage = qImage;
            imageUrl = QUrl(QString("image://imageProvider/popEye?%1").arg(imageCounter++));
            emit popEyeImageReady(imageUrl);
        }else if (target == "Box2") {
            box2Image = qImage;
            imageUrl = QUrl(QString("image://imageProvider/Box2?%1").arg(imageCounter++));
            emit box2ImageReady(imageUrl);
        } else if (target == "Main") {
            // if(ImageProcessor::getInstance().captureImgMode() != ImageProcessor::CapPause)
            // {
                m_mainImage = image;
                mainImage = qImage;
                m_mainStep = step;
                imageUrl = QUrl(QString("image://imageProvider/Main?%1").arg(imageCounter++));
                if(getImgInfo(image, m_imagemainInfo))
                    emit imageMainInfoChanged();
                emit mainImageReady(imageUrl);
            // }
        }
    }
    else
    {
        QUrl imageUrl(QString("image://imageProvider/empty?%1").arg(imageCounter++));
        if (target == "Box1") {
            emit box1ImageReady(imageUrl);
        } else if (target == "Box2") {
            emit box2ImageReady(imageUrl);
        } else if (target == "Main") {
            // if(ImageProcessor::getInstance().captureImgMode() != ImageProcessor::CapPause)
                emit mainImageReady(imageUrl);
        }
    }
}

void ImageProvider::getMainDisplay(cv::Mat &img, ImageDisplay &step)
{
    img = m_mainImage;
    step = m_mainStep;
}

QImage ImageProvider::convertToQImage(const cv::Mat& mat) {
    if (mat.empty()) {
        LogWarning << "Empty matrix cannot be converted to QImage";
        return QImage();
    }
    switch (mat.type()) {
    case CV_8UC1:
        return QImage(mat.data, mat.cols, mat.rows, static_cast<int>(mat.step), QImage::Format_Grayscale8);
    case CV_8UC3:
        return QImage(mat.data, mat.cols, mat.rows, static_cast<int>(mat.step), QImage::Format_RGB888).rgbSwapped();
    case CV_8UC4:
        return QImage(mat.data, mat.cols, mat.rows, static_cast<int>(mat.step), QImage::Format_ARGB32);
    default:
        LogWarning << "Unsupported image format";
        return QImage();
    }
}
